package com.lin.controller.userinfo;


import cn.hutool.core.util.ObjectUtil;
import com.lin.base.Result;
import com.lin.entity.*;
import com.lin.security.util.SecurityUtil;
import com.lin.service.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author 林伟鸿
 * @since 2022-04-16
 */
@RestController
@RequestMapping("/api/userinfo")
public class UserInfoController {
    @Resource
    TalentsService talentsService;
    @Resource
    GroupInformationService groupInformationService;
    @Resource
    TalentJoinService talentJoinService;
    @Resource
    ProjectInviteTalentsService projectInviteTalentsService;
    @Resource
    ProjectOutsideService projectOutsideService;
    @Resource
    ProjectStatusService projectStatusService;
    @Resource
    VoteService voteService;

    /**
     * 获取个人信息
     * @return
     */
    @GetMapping("/get")
    public Result getTalents() {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        Talents talents = talentsService.getById(currentUserUid);
        if (ObjectUtil.isNull(talents)) {
            return Result.fail("请先完善个人信息");
        }
        return Result.ok(talents, "查询成功");
    }


    @PostMapping("/update")
    public Result updateTalents(@RequestBody @Validated Talents talents) {
        talents.setUid(SecurityUtil.getCurrentUserUid());
        talentsService.saveOrUpdate(talents);
        return Result.ok("更新成功");
    }

    /**
     * 获取技能列表
     *
     */
    @GetMapping("/skills")
    public Result getSkills() {
        Talents talents = talentsService.getById(SecurityUtil.getCurrentUserUid());
        return Result.ok(talents.getTalentTag(), "查询成功");
    }

    /**
     * 获取偏好领域
     *
     */
    @GetMapping("/prefs")
    public Result getPrefs() {
        Talents talents = talentsService.getById(SecurityUtil.getCurrentUserUid());
        return Result.ok(talents.getPref(),"查询成功");
    }

    /**
     * 查看我的项目列表
     */
    @GetMapping("/projects")
    public Result getMyProjects() {
        int currentUserUid = SecurityUtil.getCurrentUserUid();
        List<Map<String, Object>> myProjects = talentsService.getMyProjects(currentUserUid);
        return Result.ok(myProjects, "查询成功");
    }

    /**
     * 获取项目团队信息
     *
     * @param projectId 项目Id
     * @return
     */
    @GetMapping("/group")
    public Result groupInfo(@RequestParam String projectId) {
        GroupInformation groupInformation = null;
        try {
            groupInformation = groupInformationService.queryGroupInfo(projectId);
            return Result.ok(groupInformation, "查询成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 获取邀请列表
     * 个人收到团队发出的邀请
     * 操作表：project_invite_talents
     */
    @GetMapping("/invites")
    public Result invitesList() {
        List<ProjectInviteTalents> inviteTalentsList = projectInviteTalentsService.getInvitesList();
        return Result.ok(inviteTalentsList, "查询成功");
    }

    /**
     * 获取加入列表
     * 队长收到别人的加入申请
     * 操作表：talent_join
     */
    @GetMapping("/joins")
    public Result joinsList() {
        return Result.ok(projectOutsideService.getJoinsList(), "查询成功");
    }

    /**
     * 加入团队处理
     */
    @PostMapping("/join_process")
    public Result joinProcess(@RequestBody TalentJoin talentJoin) {
        try {
            talentJoinService.joinProcess(talentJoin);
            return Result.ok("操作成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }

    }

    /**
     * 邀请入队处理
     */
    @PostMapping("/invite_process")
    public Result inviteProcess(@RequestBody ProjectInviteTalents projectInviteTalents) {
        try {
            projectInviteTalentsService.inviteProcess(projectInviteTalents);
            return Result.ok("操作成功");
        } catch (Exception e) {
            return Result.fail(e.getMessage());
        }
    }

    /**
     * 队长发起投票
     * @param projectStatus p
     * @return
     */
    @PostMapping("/start_vote")
    public Result startVote(@RequestBody @Validated ProjectStatus projectStatus){
        projectStatusService.startVote(projectStatus);
        return Result.ok("发起成功");
    }

    /**
     * 投票
     * @param vote vote
     * @return
     */
    @PostMapping("/vote")
    public Result vote(@RequestBody Vote vote) {
        voteService.vote(vote);
        return Result.ok("投票成功");
    }

    /**
     * 查询投票结果
     * @param projectId 项目Id
     * @return
     */
    @GetMapping("/vote_state")
    public Result voteState(@RequestParam String projectId) {
        ProjectStatus projectStatus = projectStatusService.getById(projectId);
        if (projectStatus == null) return Result.fail("此项目没有正在进行投票！");
        return Result.ok(projectStatus,"查询成功");
    }


}
